package com.gxnu.edu.cn.ttxxt.service.Impl;

import com.gxnu.edu.cn.ttxxt.entity.GroupsMembers;
import com.gxnu.edu.cn.ttxxt.mapper.GroupMemberRepository;
import com.gxnu.edu.cn.ttxxt.service.GroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class GroupMemberServiceImpl implements GroupMemberService {

    @Autowired
    private GroupMemberRepository groupMemberRepository;

    @Override
    @Transactional
    public GroupsMembers addGroupMember(GroupsMembers groupsMembers) {
        // 参数校验
        if (groupsMembers == null || groupsMembers.getGroupId() == null
                || groupsMembers.getUserId() == null) {
            throw new IllegalArgumentException("群成员参数不完整");
        }

        // 检查是否已在群中
        if (groupMemberRepository.existsByUserIdAndGroupId(
                groupsMembers.getUserId(), groupsMembers.getGroupId())) {
            throw new IllegalArgumentException("用户已在群组中");
        }

        // 设置默认值
        groupsMembers.setRole(groupsMembers.getRole() != null ? groupsMembers.getRole() : 1); // 默认普通成员
        groupsMembers.setJoinTime(new Date());

        groupMemberRepository.insert(groupsMembers);
        return groupsMembers;
    }

    @Override
    public List<GroupsMembers> getGroupMembers(Long groupId) {
        // 参数校验
        if (groupId == null) {
            throw new IllegalArgumentException("群组ID不能为空");
        }

        return groupMemberRepository.findByGroupId(groupId);
    }

    @Override
    public List<GroupsMembers> getUserGroups(Long userId) {
        // 参数校验
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        return groupMemberRepository.findByUserId(userId);
    }

    @Override
    @Transactional
    public void removeMember(Long groupId, Long userId) {
        // 参数校验
        if (groupId == null || userId == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        int affected = groupMemberRepository.deleteByGroupIdAndUserId(groupId, userId);
        if (affected == 0) {
            throw new IllegalArgumentException("群成员不存在");
        }
    }

    @Override
    @Transactional
    public void updateMemberRole(GroupsMembers member) {
        // 参数校验
        if (member == null || member.getGroupMemberId() == null
                || member.getRole() == null) {
            throw new IllegalArgumentException("参数不完整");
        }

        int affected = groupMemberRepository.update(member);
        if (affected == 0) {
            throw new RuntimeException("更新群成员角色失败");
        }
    }

    @Override
    @Transactional
    public void dissolveGroup(Long groupId) {
        // 参数校验
        if (groupId == null) {
            throw new IllegalArgumentException("群组ID不能为空");
        }

        groupMemberRepository.deleteByGroupId(groupId);
    }

    @Override
    public List<GroupsMembers> getGroupAdmins(Long groupId) {
        // 参数校验
        if (groupId == null) {
            throw new IllegalArgumentException("群组ID不能为空");
        }

        return groupMemberRepository.findByGroupIdAndRole(groupId, 2); // 2表示管理员
    }

    @Override
    public int countGroupMembers(Long groupId) {
        // 参数校验
        if (groupId == null) {
            throw new IllegalArgumentException("群组ID不能为空");
        }

        return groupMemberRepository.countByGroupId(groupId);
    }

    @Override
    public List<GroupsMembers> getUserGroupsByRole(Long userId, Integer role) {
        // 参数校验
        if (userId == null || role == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        return groupMemberRepository.findByUserIdAndRole(userId, role);
    }
}